package jp.gr.java_conf.yuzu_project.parts;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;

import jp.gr.java_conf.yuzu_project.common.CommonUtil;
import jp.gr.java_conf.yuzu_project.common.SaveDataStock;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * クイックセーブ・ロード
 * @author ゆずプロジェクト
 *
 */
public class QuickSaveLoadUtil {
	
	private Context mContext;
	private static final int SAVE_POSITION = 21;
	
	public QuickSaveLoadUtil(Context context) {
		mContext = context;
	}

	/**
	 * クイックセーブ
	 * @param saveDataStock セーブデータ
	 * @return 成功:true/失敗:false
	 */
	public boolean quickSave(SaveDataStock saveDataStock) {
		boolean saveFlg = false;
		saveFlg = dataSave(saveDataStock);
		return saveFlg;
	}
	
	/**
	 * クイックロード
	 * @return ロードデータ
	 */
	public SaveDataStock quickLoad() {
		SaveDataStock saveData = null;
		saveData = makeDataList();
		return saveData;
	}
	
	/**
	 * DB保存
	 * @return 成功:true/失敗:false
	 */
	private synchronized boolean dataSave(SaveDataStock saveData) {
		boolean saveFlg = true;
		
		SQLiteHelper helper = new SQLiteHelper(mContext);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			try {
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(saveData);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			byte[] value = baos.toByteArray();
			
			cv.put(CommonUtil.SAVE_COLUMN, value);
			
			String[] whereArgs = new String[1];
			whereArgs[0] = String.valueOf(SAVE_POSITION);
			int save = db.update(CommonUtil.TABLE_NAME, cv, "_id = ?", whereArgs);
			
			if (save == -1) {
				saveFlg = false;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			
			if (db != null) {
				db.close();
				db = null;
			}
		}
		
		return saveFlg;
	}
	
	/**
	 * DB検索
	 * @return セーブデータ
	 */
	private synchronized SaveDataStock makeDataList() {
		
		SQLiteHelper helper = new SQLiteHelper(mContext);
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = null;
		SaveDataStock saveData = null;
		String[] whereArgs = new String[1];
		whereArgs[0] = String.valueOf(SAVE_POSITION);
		
		try {
			cursor = db.query(CommonUtil.TABLE_NAME, null, "_id = ?", whereArgs, null, null, null);
			cursor.moveToFirst();
			int index = cursor.getColumnIndex(CommonUtil.SAVE_COLUMN);
			
			do {
				byte[] data = cursor.getBlob(index);
				ByteArrayInputStream bais = new ByteArrayInputStream(data);
				try {
					ObjectInputStream ois = new ObjectInputStream(bais);
					saveData = (SaveDataStock) ois.readObject();
				} catch (OptionalDataException e) {
					e.printStackTrace();
				} catch (StreamCorruptedException e) {
					e.printStackTrace();
				}catch (ClassNotFoundException e){
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			} while (cursor.moveToNext());

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		
		return saveData;
	}
}
